package xworker.lang.task.rule;

import org.xmeta.ActionContext;
import org.xmeta.Thing;
import xworker.lang.actions.code.FakeGroovyAction;
import xworker.lang.util.UtilData;

public class RuleTaskActions {
    public static void start(ActionContext actionContext){
        Thing self = actionContext.getObject("self");
        RuleTask ruleTask = RuleTask.getRuleTask(self, actionContext);
        ruleTask.start();
    }

    public static void stop(ActionContext actionContext){
        Thing self = actionContext.getObject("self");
        RuleTask ruleTask = RuleTask.getRuleTask(self, actionContext);
        ruleTask.setStop(true);
    }

    public static void reset(ActionContext actionContext){
        Thing self = actionContext.getObject("self");
        RuleTask ruleTask = RuleTask.getRuleTask(self, actionContext);
        ruleTask.reset();
    }

    public static RuleTask getRuleTask(ActionContext actionContext){
        Thing self = actionContext.getObject("self");
        return RuleTask.getRuleTask(self, actionContext);
    }

    //xworker.lang.task.rule.GroovyRule/@actions/@isActive
    public static Object isActiveGroovy(ActionContext actionContext){
        Thing self = actionContext.getObject("self");

        FakeGroovyAction fakeGroovyAction = self.getData("isActiveCode");
        if(fakeGroovyAction == null) {
            fakeGroovyAction = new FakeGroovyAction(self, "isActiveCode");
            self.setData("isActiveCode", fakeGroovyAction);
        }

        return fakeGroovyAction.execute(actionContext);
    }

    //xworker.lang.task.rule.GroovyRule/@actions/@doTask
    public static Object doTaskGroovy(ActionContext actionContext){
        Thing self = actionContext.getObject("self");

        FakeGroovyAction fakeGroovyAction = self.getData("doTaskCode");
        if(fakeGroovyAction == null) {
            fakeGroovyAction = new FakeGroovyAction(self, "doTaskCode");
            self.setData("doTaskCode", fakeGroovyAction);
        }

        return fakeGroovyAction.execute(actionContext);
    }

    //xworker.lang.task.rule.OnSuccess/@actions/@isActive
    public static Object isActiveOnSuccess(ActionContext actionContext){
        return UtilData.isTrue(actionContext.getObject("parentSucess"));
    }

    //xworker.lang.task.rule.OnException/@actions/@isActive
    public static Object isActiveOnException(ActionContext actionContext){
        return !UtilData.isTrue(actionContext.getObject("parentSucess"));
    }
}
